package com.wobito.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wobito.common.utils.StringUtils;
import com.wobito.mapper.PersonMapper;
import com.wobito.pojo.bean.Person;
import com.wobito.pojo.dto.person.PersonDTO;
import com.wobito.pojo.vo.person.PersonVO;
import com.wobito.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lambyang
 * @since 2024-08-26
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public List<PersonDTO> selectPersonList(PersonVO personVO) {
        List<PersonDTO> personDTOS = new ArrayList<>();
        List<Person> personList = personMapper.selectList(new LambdaQueryWrapper<Person>()
                .like(StringUtils.isNotEmpty(personVO.getQueryName()), Person::getName, personVO.getQueryName())
                .eq(personVO.getIsDeleted() != null, Person::getIsDeleted, personVO.getIsDeleted()));
        if (CollectionUtil.isNotEmpty(personList)) {
            personDTOS = personList.stream().map(p -> {
                PersonDTO personDTO = BeanUtil.copyProperties(p, PersonDTO.class);
                return personDTO;
            }).collect(Collectors.toList());
        }
        return personDTOS;
    }

    @Override
    public Person selectPersonByCode(String code) {
        return personMapper.selectOne(new LambdaQueryWrapper<Person>().or(w -> {
            w.eq(Person::getIcCode, code);
        }));
    }
}
